// pages/shop/info.js
var app = getApp();
Page({
  data: {
  },
  onLoad: function (options) {
  },
  onShow: function () {
    var that = this;
    //TODO: 可以直接本地读取了
    app.func.req('shops/show', {
      id: wx.getStorageSync('shop').id
    }, 'GET', function (res) {
      var time = res.result.open_time.split("-");
      that.setData({
        shop: res.result,
        open_time: time[0],
        end_time: time[1]
      });
    }, {
        'token': wx.getStorageSync('token')
      });
      //本地读取没有logo信息 ？？
      // var shop = wx.getStorageSync('shop');
      // var time = shop.open_time.split("-");
      // that.setData({
      //   shop: shop,
      //   open_time: time[0],
      //   end_time: time[1]
      // });
  },
  openTime: function(e){
    this.setData({
      open_time: e.detail.value
    })
  },
  endTime: function (e) {
    this.setData({
      end_time: e.detail.value
    })
  },
  chooseImage: function () {
    var that = this;

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({title: '准备上传...', mask: true});
        app.func.upload('file/upload', tempFilePaths[0], {}, function(res){
          var res =  JSON.parse(res);
            if(res.code == 'success'){
              var shop = that.data.shop;
              shop.logo = res.result[0];

              that.setData({
                shop: shop
              });

              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: '上传失败',
                icon: 'success',
                duration: 2000
              })
            }
        },{
          token: wx.getStorageSync('token')
        });
        // wx.uploadFile({
        //   url: 'http://cm.shop.com/v1/file/upload',
        //   filePath: tempFilePaths[0],
        //   name: 'file',
        //   formData:{},
        //   header: {
        //     'token': wx.getStorageSync('token')
        //   },
        //   success: function(res){
        //     wx.hideLoading();
        //     console.log(res)
        //     if (res.errMsg == 'uploadFile:ok') {
        //
        //       var res =  JSON.parse(res.data);
        //       if(res.code == 'success'){
        //         var shop = that.data.shop;
        //         shop.logo = res.result[0];
        //
        //         that.setData({
        //           shop: shop
        //         });
        //
        //         wx.showToast({
        //           title: '上传成功',
        //           icon: 'success',
        //           duration: 2000
        //         })
        //       }
        //     } else {
        //       wx.showToast({
        //         title: '上传失败',
        //         icon: 'success',
        //         duration: 2000
        //       })
        //     }
        //     wx.hideLoading();
        //   }
        // });
      }
    });
  },
  save: function (e) {
    var that = this;
    app.func.req('shops/update', {
      name: e.detail.value.name,
      addr: e.detail.value.addr,
      open_time: that.data.open_time+'-'+that.data.end_time,
      descrip: e.detail.value.descrip,
      logo: that.data.shop.logo,
      wechat: e.detail.value.wechat,
      announcement: e.detail.value.announcement,
      kfphone: e.detail.value.kfphone
    }, 'POST', function (res) {
      console.log(res)
      if (res.code == 'success') {
        wx.showToast({
          title: '保存成功',
          duration: 3000,
          success: function () {
            wx.switchTab({
              url: './shop'
            })
          }
        });

        //TODO:更新本地缓存
        var shop = wx.getStorageSync('shop');
        shop.name = e.detail.value.name;
        shop.addr = e.detail.value.addr;
        shop.open_time = that.data.open_time+'-'+that.data.end_time,
        shop.descrip = e.detail.value.descrip;
        shop.logo = e.detail.value.logo;
        shop.announcement = e.detail.value.announcement;
        wx.setStorageSync('shop', shop);
      } else if (res.msg == 'shops.name.exist') {
        wx.showToast({
          title: '店铺名已存在',
          image: '/images/fail.png',
          duration: 3000
        })
      } else {
        wx.showToast({
          title: '保存失败',
          image: '/images/fail.png',
          duration: 3000
        })
      }
    }, {
        'token': wx.getStorageSync('token')
      });
  }
})
